package sketch.specs;

import java.util.Collection;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener;

import sketch.ast.ASTUtils;
import sketch.util.Checker;

public class BlockRemoveVisitor extends NaiveASTFlattener {

	public final Collection<Block> blocksToRemove;
	
	public final AST ast;
	
	public BlockRemoveVisitor(Collection<Block> blocksToRemove) {
		Checker.checkNull(blocksToRemove, "The blocksToRemove argument could not be null.");
		this.blocksToRemove = blocksToRemove;
		ast = ASTUtils.getAST(blocksToRemove);
	}
	
	@Override
	public boolean visit(Block block) {
		if(block.getAST() == ast) {
			boolean shouldRemove = false;
			for(Block b : blocksToRemove) {
				if(b.getStartPosition() == block.getStartPosition()) {
					shouldRemove = true;
					break;
				}
			}
			if(shouldRemove) {
				//XXX a better way may go to its parent node and delete this block
				block.statements().clear();
			}
		} else {
			System.err.println("not the same ast @ BlockRemoverVisitor.");
		}
		return super.visit(block);
	}
}